/*
 * CAN.h
 *
 *  Created on: 2021年8月22日
 *      Author: bingshan
 */

#ifndef CAN_H_
#define CAN_H_


#include <bits/stdc++.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>

using namespace std;

/*
 * sudo ip link set can0 type can bitrate 500000
 * sudo ip link set up can0
 * sudo ip link set down can0
*/

class CAN
{
private:
    int socketCan_handler;
    static CAN* instance;
    thread *master_thread;

    void _master_exec();
protected:
    CAN();

public:
    ~CAN();
    static CAN* getInstance() {
        if(instance==nullptr) {
            instance = new CAN();
        }
        return instance;
    };
    //set CAN Filter
    void SetFilter(int val);
    void SetFilter(vector<int> &vals);
    int sendMessage(int id, const vector<unsigned char> &vals);

    /* this Function will block the current process
    *  return : CAN packet id
    *  param  :
    *           vals : should be a empty vector
    *
    */
    int getMessage(vector<unsigned char> &vals);
};


#endif /* CAN_H_ */
